package com.pocket.cloud.gateway.filters;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

/**
 * 路由过滤器
 * @author zhaozhile
 * <a href="https://blog.csdn.net/qq_43437874/article/details/121626379">参考</a>
 */
@Component
public class TodoGatewayFilterFactory extends AbstractGatewayFilterFactory<TodoGatewayFilterFactory.Config> {

    private static final Logger LOGGER = LoggerFactory.getLogger(TodoGatewayFilterFactory.class);

    public TodoGatewayFilterFactory() {
        super(Config.class);
    }

    // 简化配置方案。返回简化配置参数命名列表
    // Todo=nameValue,pathValue
    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("name", "path");
    }

    @Override
    public GatewayFilter apply(Config config) {
        LOGGER.info("获取 Todo 过滤器参数：{} - {}", config.getName(), config.getPath());
        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                return chain.filter(exchange);
            }
        };
    }

    public static class Config {
        private String name;
        private String path;

        public String getName() {
            return name;
        }

        public Config setName(String name) {
            this.name = name;
            return this;
        }

        public String getPath() {
            return path;
        }

        public void setPath(String path) {
            this.path = path;
        }
    }











    @Override
    public String name() {
        return super.name();
    }

    @Override
    public ShortcutType shortcutType() {
        return super.shortcutType();
    }



    @Override
    public String shortcutFieldPrefix() {
        return super.shortcutFieldPrefix();
    }
}
